/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sapra.abc;

import java.util.List;
import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.TextField;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.list.DefaultListModel;
import org.sapra.dao.CCosto;
import org.sapra.echo.Helpers;
import org.sapra.echo.wAviso;
import org.sapra.logon.dLogin;
import org.sapra.main.vPrincipal;


/**
 * Generated Echo form template
 * 
 * @author ppratsc
 */
public class vCentroCosto extends nextapp.echo2.app.ContentPane {

    public vCentroCosto(vPrincipal princ) {
        super();
        this.princ=princ;
        initComponents();
        
    }
    private SelectField mLista;
    private TextField mNombre;
    private TextField mDesc;
    private CCosto mCurrent;
    private int counter = 1;
    private vPrincipal princ;

    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        
        Column col1 = Helpers.Column("col1", new Extent(5), this);
        Row x=princ.getMenu();
        x.setAlignment(Alignment.ALIGN_CENTER);
        col1.add(x);
        Row title= Helpers.Row("Row1", new Extent(5), col1);
        title.add(Helpers.Label("CENTRO DE COSTOS.",  new Color(0x011dac), title));
        title.setAlignment(Alignment.ALIGN_CENTER);
        //Helpers.Label("LISTADO DE CENTRO DE COSTOS", Color.BLACK, col1);
        Row row1 = Helpers.Row("Row1", new Extent(5), col1);
        row1.setAlignment(Alignment.ALIGN_CENTER);
        /*Column col2 = Helpers.Column("col2", new Extent(5), row1);
        mLista = Helpers.SelectField(col2,new ActionListener(){public void actionPerformed(ActionEvent e) {Seleccion(e);}});
        mLista.setHeight(new Extent(20));
        mLista.setWidth(new Extent(400));*/

        Column col3 = Helpers.Column("col3", new Extent(5), row1);
        Helpers.Label("LISTADO DE CENTRO DE COSTOS", Color.BLACK, col3);
        mLista = Helpers.SelectField(col3,new ActionListener(){public void actionPerformed(ActionEvent e) {Seleccion(e);}});
        mLista.setHeight(new Extent(20));
        mLista.setWidth(new Extent(400));
        col3.add(mLista);
        Label lnm = Helpers.Label("Nombre:", Color.BLACK, col3);
        mNombre = Helpers.Text("", col3);
        mNombre.setWidth(new Extent(250));
        Label ldd = Helpers.Label("Descripcion:", Color.BLACK, col3);
        mDesc = Helpers.Text("", col3);
        mDesc.setWidth(new Extent(250));
        mDesc.setHeight(new Extent(120));

        Row row3 = Helpers.Row("Row3", new Extent(5), col1);
        row3.setAlignment(Alignment.ALIGN_CENTER);
        Button bt3 = Helpers.Button("NUEVO", row3, new ActionListener(){public void actionPerformed(ActionEvent e) {MenuNuevo(e);}});
        Button bt1 = Helpers.Button("GUARDAR", row3, new ActionListener(){public void actionPerformed(ActionEvent e) {MenuGuardar(e);}});
        Button bt2 = Helpers.Button("BORRAR", row3, new ActionListener(){public void actionPerformed(ActionEvent e) {MenuBorrar(e);}});
        
        mNombre.setMaximumLength(45);
        mDesc.setMaximumLength(100);
        FirstTime();
        
    }// </editor-fold>
    
    void FirstTime(){

        UpdateView();
    }

    void UpdateView(){
        //Actualiza la Lista
        List<CCosto> ccostos = dCentroCosto.getCCosto(dLogin.getEmpresa());
        mLista.setModel(new DefaultListModel(ccostos.toArray()));
        mCurrent = null;
        if(!ccostos.isEmpty())mLista.setSelectedIndex(0);
        mNombre.setText("");
        mDesc.setText("");
        mNombre.setEnabled(false);
        mDesc.setEnabled(false);
    }
    
    void MenuGuardar(ActionEvent e){
        int flag=1;
        if(mCurrent == null){
            mCurrent=new CCosto();
            flag=0;
        }
        if(mNombre.getText().trim().equals("")){
            add(new wAviso("ERROR","FAVOR DE ESCRIBIR UN NOMBRE"));
            return;
        }
         if(mDesc.getText().trim().equals("")){
            add(new wAviso("ERROR","FAVOR DE ESCRIBIR UNA DESCRIPCION"));
            return;
        }
        mCurrent.setNombre(mNombre.getText());
        mCurrent.setDescripcion(mDesc.getText());
        if(flag==0){
            dCentroCosto.Nuevo(mCurrent);
            add(new wAviso("EXITO","REGISTRO AGREGADO CORRECTAMENTE"));}
        else{
            dCentroCosto.SaveCCosto(mCurrent);
                    add(new wAviso("EXITO","REGISTRO AGREGADO CORRECTAMENTE"));}
        UpdateView();
    }

    void MenuBorrar(ActionEvent e){
        if(mCurrent == null){
             add(new wAviso("ERROR","DEBE SELECCIONAR UN REGISTRO"));
        return;
        }

        int res=dCentroCosto.Delete(mCurrent);
        if (res == 1){
            add(new wAviso("ERROR","NO SE PUEDE BORRAR EL CENTRO DE COSTOS"
                    + " DEBIDO A QUE TIENE PRACTICANTES ASOCIADOS"));
            return;
        }else{
            add(new wAviso("EXITO","REGISTRO ELIMINADO CORRECTAMENTE"));
        }
        UpdateView();

    }

     void MenuNuevo(ActionEvent e){
        mNombre.setEnabled(true);
        mDesc.setEnabled(true);
        mNombre.setText("");
        mDesc.setText("");
        
    }

     void Seleccion(ActionEvent e){
        mCurrent = (CCosto) mLista.getSelectedItem();
        if(mCurrent == null) return;
        mNombre.setText(mCurrent.getNombre()+mCurrent.getCentroCostoId());
        mDesc.setText(mCurrent.getDescripcion());
        mNombre.setEnabled(true);
        mDesc.setEnabled(true);

     }


}

